package com.hdoit.ares.logical.cust.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.cust.dao.ICustPosterDao;
import com.hdoit.ares.logical.cust.model.CustPoster;
import com.hdoit.ares.logical.viewmodel.cust.CustPosterSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      xieqixiu
 * @email       
 * @createtime  2018年03月10日 16:15:04
 * @comment
 */
@Component
public class CustPosterDaoImpl extends SuperDao<CustPoster> implements ICustPosterDao{

	@Override
	public List<CustPoster> queryListBack(CustPosterSearch custPosterSearch,PagerModel pagerModel) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression = getFinderExpression(finderExpression, custPosterSearch);
		
		return queryList(finderExpression, pagerModel);
	}

	
	@Override
	public List<CustPoster> queryListApi(CustPosterSearch custPosterSearch) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression = getFinderExpression(finderExpression, custPosterSearch);
		
		return queryList(finderExpression);
	}
	
	private IFinderExpression getFinderExpression(IFinderExpression finderExpression,CustPosterSearch custPosterSearch){
			
		finderExpression.select("*")
						.from(CustPoster.class);
		
		if(!StringUtils.isEmpty(custPosterSearch.getPosterType())){
			finderExpression.andWhere("posterType=#{posterType}")
							.setParam("posterType", custPosterSearch.getPosterType());
		}
		if(!StringUtils.isEmpty(custPosterSearch.getStartDate())){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},createDate)>=0")
							.setParam("startDate",custPosterSearch.getStartDate());
		}
		
		if(!StringUtils.isEmpty(custPosterSearch.getEndDate())){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,createDate,#{endDate})>=0")
							.setParam("endDate", DateUtils.addDay(1, custPosterSearch.getEndDate()));
		}
		
		finderExpression.addOrder("createDate", OrderEnum.desc);
		
		if(!StringUtils.isEmpty(custPosterSearch.getPageSize())){
			if(StringUtils.isEmpty(custPosterSearch.getPageNum())){
				finderExpression.limit(0, custPosterSearch.getPageSize());
			}else{
				int start =(custPosterSearch.getPageNum()-1)*custPosterSearch.getPageSize();
				finderExpression.limit(start, custPosterSearch.getPageSize());
			}
		}
		return finderExpression;
	}
	
}